Classes and Functions

Before creating any KonsolScript program, here's is one very important information that you should know – KonsolScript is NOT Object-Oriented Programming Language, yet.

KonsolScript Classes

Classes in KonsolScript differ in what you already know in other Object-Oriented Programming languages, like C++/Java.

In KonsolScript, a lot of built-in functionalities are already provided to easily create a 2D game. To help us remember at least some of those, these functions were categorized in what KonsolScript Developers call as class or types. But the word type is more known to programmer's lingoes as variable.

So, simply imagine KonsolScript classes as Namespace. Meaning, you cannot access any built-in KonsolScript function without typing the name of the Class it belongs.

See KonsolScript classes

Syntax is also, somewhat, different. We use a colon to access the function.

KonsolScript_Class:KonsolScript_Function([some_parameter/s])

Given the syntax above, we can sample out the Screen Class' Render function.

  Screen:Render()

So why colon and not the usual dot ( . )? Because there's a plan to support real classes. Soon KonsolScript will be Object-Oriented.

KonsolScript Functions

All built-in functions of KonsolScript are in a void-like format.

//a C/C++ way of using a function
  int x;
  x = rand();

In other language, like C/C++, a non-void function is accesed thru an equal sign.


//a KonsolScript way of using a function
  Var:Number x;
  Math:Random(0,10,x)

In KonsolScript, a non-void function is accesed by providing a variable as the last parameter of the function.

If you hate doing like so, we can make a work around on this. We will write a user-defined function-wrapper. How? Read on.

User-Defined Functions

A user-defined function is your way to make your program more organized. Look at the sample below.

#include "console"
 
Var:String fname,mname,lname;
Var:Number submit,flag;
 
function main() {
  clearScreen()
  if (submit == 1) {
    flag=0;
    if (fname == "") { writeLine("No first name!") flag=1; }
    if (mname == "") { writeLine("No middle name!") flag=1; }
    if (lname == "") { writeLine("No last name!") flag=1; }
    if (flag == 0) {
      writeLine("Hello, " + fname + " " + mname + " " + lname + "!")
    } else {
      write("First Name : ") readString(fname)
      write("Middle Name: ") readString(mname)
      write("Last Name  : ") readString(lname)
      submit = 1;
      main()
    }
  } else {
    write("First Name : ") readString(fname)
    write("Middle Name: ") readString(mname)
    write("Last Name  : ") readString(lname)
    submit = 1;
    main()
  }
  end()
}


The program above will run without problem but we could optimize it to be more organized.

#include "console"
 
Var:String fname,mname,lname;
Var:Number submit,flag;
 
function main() {
  clearScreen()
  if (submit == 1) {
    flag=0;
    if (fname == "") { writeLine("No first name!") flag=1; }
    if (mname == "") { writeLine("No middle name!") flag=1; }
    if (lname == "") { writeLine("No last name!") flag=1; }
    if (flag == 0) {
      writeLine("Hello, " + fname + " " + mname + " " + lname + "!")
    } else {
      getData()
    }
  } else {
    getData()
  }
  end()
}
 
function getData() {
  write("First Name : ") readString(fname)
  write("Middle Name: ") readString(mname)
  write("Last Name  : ") readString(lname)
  submit = 1;
  main()
}

The sample above showcases a user-defined function, which was a part of the code. But instead of repeatedly typing the whole code, we simply have to call the getData function.

Or course, the above samples on user-defined functions are not practical. But there, surely, are some cases where this simply happens. This is just to sample out the use of a user-defined function. Another is by making a user-defined function-wrapping one.

function main() {
  Var:Number y;
  y = Math_Random(0,10);
}
 
//a user-defined function-wrapper for Math:Random
function Math_Random(Number nMin, Number nMax):Number {
  Var:Number x;
  Math:Random(nMin,nMax,x)
  return x;
}

The Math_Random contains a code that calls out the KonsolScript's built-in Random function of Math class. In function main, it seemed as if Math_Random is a built-in function that can be used in a non-void format.


www.konsolscript.org
© 2005-2011 KonsolScript Labs | All Rights Reversed | Licensed under GNU GPL | Designed by Mj Mendoza IV
http://www.sourceforge.net